<?php
/**
 * Student data mapper
 *
 * Implements the Data Mapper design pattern
 *
 * @version $Id: StudentMapper.php 73 2010-04-25 00:30:44Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Student_Model_DbTable_Student
 * @package student
 * @subpackage Model
 */
class Student_Model_StudentMapper
{

    protected $_dbTable = null;

    /**
     * Specify Zend_Db_Table instance to use for data operations
     *
     * @param Zend_Db_Table_Abstract $$dbTable
     * @return Student_Model_StudentMapper
     */
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    /**
     * Get registered Zend_Db_Table instance
     *
     * Lazy loads Student_Model_Student if no instance registered
     *
     * @return Zend_Db_Table_Abstract
     */
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Student_Model_DbTable_Student');
        }
        return $this->_dbTable;
    }

    /**
     * Find a student entry by a key
     *
     * @param int $id
     * @param Student_Model_Student $student
     * @return void
     */
    public function save($student)
    {
        $data = array(
        	'name' => $student->getName(),
        	'english_name' => $student->getEnglish_name(),
        	'sex' => $student->getSex(),
        	'birthday' => $student->getBirthday(),
        	'blood' => $student->getBlood(),
        	'kind' => $student->getKind(),
        	'kind_memo' => $student->getKind_memo(),
        	'indigene_area' => $student->getIndigene_area(),
        	'indigene_clan' => $student->getIndigene_clan(),
        	'nationality' => $student->getNationality(),
        	'identification' => $student->getIdentification(),
        	'identification_number' => $student->getIdentification_number(),
        	'colony' => $student->getColony(),
        	'registered_address' => $student->getRegistered_address(),
        	'address' => $student->getAddress(),
        	'address_zip' => $student->getAddress_zip(),
        	'registered_county' => $student->getRegistered_county(),
        	'registered_town' => $student->getRegistered_town(),
        	'registered_village' => $student->getRegistered_village(),
        	'registered_neighbor' => $student->getRegistered_neighbor(),
        	'registered_road' => $student->getRegistered_road(),
        	'registered_section' => $student->getRegistered_section(),
        	'registered_lane' => $student->getRegistered_lane(),
        	'registered_alley' => $student->getRegistered_alley(),
        	'registered_no' => $student->getRegistered_no(),
        	'registered_extend' => $student->getRegistered_extend(),
        	'registered_floor' => $student->getRegistered_floor(),
        	'registered_floor_extend' => $student->getRegistered_floor_extend(),
        	'registered_other' => $student->getRegistered_other(),
        	'phone_home' => $student->getPhone_home(),
        	'phone_registered' => $student->getPhone_registered(),
        	'phone_mobile' => $student->getPhone_mobile(),
        	'class_property' => $student->getClass_property(),
        	'special_kind' => $student->getSpecial_kind(),
        	'special_class_kind' => $student->getSpecial_class_kind(),
        	'special_class_property' => $student->getSpecial_class_property(),
        	'preschool_qualification' => $student->getPreschool_qualification(),
        	'preschool_educational_id' => $student->getPreschool_educational_id(),
        	'preschool_name' => $student->getPreschool_name(),
        	'elementary_school_qualification' => $student->getElementary_school_qualification(),
        	'elementary_school_educational_id' => $student->getElementary_school_educational_id(),
        	'elementary_school_name' => $student->getElementary_school_name(),
        	'enroll_year' => $student->getEnroll_year(),
        	'enroll_school' => $student->getEnroll_school(),
        	'address_move_in' => $student->getAddress_move_in(),
        	'born_place' => $student->getBorn_place(),
        	'father_sn' => $student->getFather_sn(),
        	'father_relation' => $student->getFather_relation(),
        	'mother_sn' => $student->getMother_sn(),
        	'mother_relation' => $student->getMother_relation(),
        	'guardian_sn' => $student->getGuardian_sn(),
        	'guardian_relation' => $student->getGuardian_relation(),
        	'grandfather_sn' => $student->getGrandfather_sn(),
        	'grandmother_sn' => $student->getGrandmother_sn(),
        	'update_time' => $student->getUpdate_time(),
        	'update_sn' => $student->getUpdate_sn(),
        );
        if (null === ($sn= $student->getSn() )) {
            unset($data['sn']);
             $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('sn = ?' => $sn));
        }
    }

    /**
     * Save a student entry
     *
     * @param Student_Model_Student $student
     * @return void
     */
    public function find($id, $student)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $student->setSn($row->sn)
        ->setName($row->name)
        ->setEnglish_name($row->english_name)
        ->setSex($row->sex)
        ->setBirthday($row->birthday)
        ->setBlood($row->blood)
        ->setKind($row->kind)
        ->setKind_memo($row->kind_memo)
        ->setIndigene_area($row->indigene_area)
        ->setIndigene_clan($row->indigene_clan)
        ->setNationality($row->nationality)
        ->setIdentification($row->identification)
        ->setIdentification_number($row->identification_number)
        ->setColony($row->colony)
        ->setRegistered_address($row->registered_address)
        ->setAddress($row->address)
        ->setAddress_zip($row->address_zip)
        ->setRegistered_county($row->registered_county)
        ->setRegistered_town($row->registered_town)
        ->setRegistered_village($row->registered_village)
        ->setRegistered_neighbor($row->registered_neighbor)
        ->setRegistered_road($row->registered_road)
        ->setRegistered_section($row->registered_section)
        ->setRegistered_lane($row->registered_lane)
        ->setRegistered_alley($row->registered_alley)
        ->setRegistered_no($row->registered_no)
        ->setRegistered_extend($row->registered_extend)
        ->setRegistered_floor($row->registered_floor)
        ->setRegistered_floor_extend($row->registered_floor_extend)
        ->setRegistered_other($row->registered_other)
        ->setPhone_home($row->phone_home)
        ->setPhone_registered($row->phone_registered)
        ->setPhone_mobile($row->phone_mobile)
        ->setClass_property($row->class_property)
        ->setSpecial_kind($row->special_kind)
        ->setSpecial_class_kind($row->special_class_kind)
        ->setSpecial_class_property($row->special_class_property)
        ->setPreschool_qualification($row->preschool_qualification)
        ->setPreschool_educational_id($row->preschool_educational_id)
        ->setPreschool_name($row->preschool_name)
        ->setElementary_school_qualification($row->elementary_school_qualification)
        ->setElementary_school_educational_id($row->elementary_school_educational_id)
        ->setElementary_school_name($row->elementary_school_name)
        ->setEnroll_year($row->enroll_year)
        ->setEnroll_school($row->enroll_school)
        ->setAddress_move_in($row->address_move_in)
        ->setBorn_place($row->born_place)
        ->setFather_sn($row->father_sn)
        ->setFather_relation($row->father_relation)
        ->setMother_sn($row->mother_sn)
        ->setMother_relation($row->mother_relation)
        ->setGuardian_sn($row->guardian_sn)
        ->setGuardian_relation($row->guardian_relation)
        ->setGrandfather_sn($row->grandfather_sn)
        ->setGrandmother_sn($row->grandmother_sn)
        ->setUpdate_time($row->update_time)
        ->setUpdate_sn($row->update_sn)
        ;
    }

    /**
     * Fetch all student entries
     *
     * @return array
     */
    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Student_Model_Student();
            $entry->setSn($row->sn)
            ->setName($row->name)
            ->setEnglish_name($row->english_name)
            ->setSex($row->sex)
            ->setBirthday($row->birthday)
            ->setBlood($row->blood)
            ->setKind($row->kind)
            ->setKind_memo($row->kind_memo)
            ->setIndigene_area($row->indigene_area)
            ->setIndigene_clan($row->indigene_clan)
            ->setNationality($row->nationality)
            ->setIdentification($row->identification)
            ->setIdentification_number($row->identification_number)
            ->setColony($row->colony)
            ->setRegistered_address($row->registered_address)
            ->setAddress($row->address)
            ->setAddress_zip($row->address_zip)
            ->setRegistered_county($row->registered_county)
            ->setRegistered_town($row->registered_town)
            ->setRegistered_village($row->registered_village)
            ->setRegistered_neighbor($row->registered_neighbor)
            ->setRegistered_road($row->registered_road)
            ->setRegistered_section($row->registered_section)
            ->setRegistered_lane($row->registered_lane)
            ->setRegistered_alley($row->registered_alley)
            ->setRegistered_no($row->registered_no)
            ->setRegistered_extend($row->registered_extend)
            ->setRegistered_floor($row->registered_floor)
            ->setRegistered_floor_extend($row->registered_floor_extend)
            ->setRegistered_other($row->registered_other)
            ->setPhone_home($row->phone_home)
            ->setPhone_registered($row->phone_registered)
            ->setPhone_mobile($row->phone_mobile)
            ->setClass_property($row->class_property)
            ->setSpecial_kind($row->special_kind)
            ->setSpecial_class_kind($row->special_class_kind)
            ->setSpecial_class_property($row->special_class_property)
            ->setPreschool_qualification($row->preschool_qualification)
            ->setPreschool_educational_id($row->preschool_educational_id)
            ->setPreschool_name($row->preschool_name)
            ->setElementary_school_qualification($row->elementary_school_qualification)
            ->setElementary_school_educational_id($row->elementary_school_educational_id)
            ->setElementary_school_name($row->elementary_school_name)
            ->setEnroll_year($row->enroll_year)
            ->setEnroll_school($row->enroll_school)
            ->setAddress_move_in($row->address_move_in)
            ->setBorn_place($row->born_place)
            ->setFather_sn($row->father_sn)
            ->setFather_relation($row->father_relation)
            ->setMother_sn($row->mother_sn)
            ->setMother_relation($row->mother_relation)
            ->setGuardian_sn($row->guardian_sn)
            ->setGuardian_relation($row->guardian_relation)
            ->setGrandfather_sn($row->grandfather_sn)
            ->setGrandmother_sn($row->grandmother_sn)
            ->setUpdate_time($row->update_time)
            ->setUpdate_sn($row->update_sn)
            ;
            $entries[] = $entry;
        }
        return $entries;
    }

    public function getClassStudent($year_semester,$class_id,$study_condition)
    {
        $arr = explode("-",$year_semester);
        $year = $arr[0];
        $semester = $arr[1];
        $grade = substr($class_id,0,1);
        $class_sn = (int)substr($class_id,2);
        $db = $this->getDbTable()->getAdapter();
        $select = $db->select()
        ->from(array("a"=>"student"),array('name','sex','sn'))
        ->join(array('b'=>'student_semester'),
			'a.sn=b.student_sn',
        array('student_id','number'))
        ->where('b.year=?',$year)
        ->where('b.semester=?',$semester)
        ->where('b.grade=?',$grade)
        ->where('b.class_sn=?',$class_sn)
        ->where('a.study_condition=?',$study_condition)
        ->order(array('b.number'));
        $res =$db->query($select);
        $arr = array();
        foreach($res as $row) {
            $arr[$row['sn']] = $row;
        }
        return $arr;

    }
}
